/*********************************************************
 *	Description:	.h file of SurFAppC. Define constants and message
 *				type.
 *	Project:		SurF
 *	Author:		Zheng Xiaolong
 *	Updated:		2012/10/23
 *********************************************************/


#ifndef BLINKTORADIO_H
#define BLINKTORADIO_H
#define VOLUME_LOGTEST 4

#define BIT_GET(x, i) ((x) & (1 << (i)))
#define BIT_SET(x, i) ((x) | (1 << (i)))
#define BIT_CLEAR(x, i) ((x) & ~(1 << (i)))

#define BITVEC_GET(x, i) (BIT_GET((x)[(i)/8], (i)%8))
#define BITVEC_SET(x, i) ((x)[(i)/8] = BIT_SET((x)[(i)/8], (i)%8))
#define BITVEC_CLEAR(x, i) ((x)[(i)/8] = BIT_CLEAR((x)[(i)/8], (i)%8))


enum {
  AM_SURFFLOODING = 6,
  AM_SYNC_MESSAGE = 12,

  AM_SURFADVMSG  = 0x30,
  AM_SURFREQMSG  = 0x31,
  AM_SURFDATAMSG = 0x32,

  STATE_RX   = 0xDA,
  STATE_FLOD = 0xDB,
  STATE_NEGO = 0xDC,
  STATE_IDLE = 0xDD,
  
  //TIMER_PERIOD_MILLI = 250,
  //TIMER_PERIOD_MILLI = 50,
  TIMER_PERIOD_MILLI = 15,
  
  SUPP_COUNTER_1 = 2,
  SUPP_COUNTER_2 = 3,
  SUPP_COUNTER_3 = 4,
  SUPP_COUNTER_4 = 5,

  BCASTCOUNTER = 1,
  BCASTPPROBABILITY = 90,
  
  LOG_BITMAP = 		0xCA,
  LOG_FLOODING = 	0xCB,
  LOG_NEGOTIATION = 0xCC,
  LOG_START = 		0xCD,
  LOG_RECE 	= 		0xCE,
  LOG_SEND 	=		0xCF,
  LOG_END 	=		0x07,
  
  MAX_REQ = 2,
  REQ_TIMEOUT = 500,
  ADV_TIMEOUT = 2000,

  MAX_PAGE = 2,
  MAX_NEIGHBOR = 10,

  MAX_PACKET = 5000,
  //PACKET_NUMBER = 200,
  PACKET_NUMBER = 48,
  BITMAP_BYTE = PACKET_NUMBER/8,
};

typedef nx_struct SurFFloodingMsg {
  nx_uint16_t seqno;
  nx_uint16_t nodeid;
  nx_uint16_t counter;
  nx_uint8_t rnd;
  nx_uint8_t data[21];
} SurFFloodingMsg;

typedef nx_struct settings_t {
	nx_uint8_t type;
	nx_uint16_t data;
	nx_uint16_t data2;
} settings_t;


typedef nx_struct SurFAdvMsg {
	nx_uint16_t seqno;
  	nx_uint16_t    sourceAddr;
  	nx_uint8_t     version;    // SurF Version
  	nx_uint8_t     type;
  	nx_uint8_t     reserved;
} SurFAdvMsg;

typedef nx_struct SurFReqMsg {
	nx_uint16_t seqno;
	nx_uint16_t    dest;
  	nx_uint16_t    sourceAddr;
  	//nx_object_id_t objid;
  	//nx_page_num_t  pgNum;
  	nx_uint8_t	 rnd;
  	nx_uint16_t    pgNum;
  	nx_uint8_t     requestedPkts[BITMAP_BYTE];
} SurFReqMsg;

typedef nx_struct SurFDataMsg {
	nx_uint16_t seqno;
	//nx_object_id_t objid;
	//nx_page_num_t  pgNum;
	nx_uint16_t	 nodeid;
	nx_uint16_t    pgNum;
	nx_uint8_t     pktNum;
	nx_uint8_t     data[20];
} SurFDataMsg;

struct neighbor {
	nx_uint16_t nodeid;
	nx_uint8_t prr;
	nx_uint16_t lastSeqno;
	//bool upper;
};



#endif
